<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=2"><meta name="theme-color" content="#222"><meta name="generator" content="Hexo 5.4.0"><link rel="apple-touch-icon" sizes="180x180" href="/hexoblog/images/apple-touch-icon-next.png"><link rel="icon" type="image/png" sizes="32x32" href="/hexoblog/images/favicon-32x32-next.png"><link rel="icon" type="image/png" sizes="16x16" href="/hexoblog/images/favicon-16x16-next.png"><link rel="mask-icon" href="/hexoblog/images/logo.svg" color="#222"><link rel="stylesheet" href="/hexoblog/css/main.css"><link rel="stylesheet" href="https://unpkg.com/@fortawesome/fontawesome-free@5.15.3/css/all.min.css"><link rel="stylesheet" href="https://unpkg.com/animate.css@3.1.1/animate.min.css"><link rel="stylesheet" href="https://unpkg.com/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css"><script class="hexo-configurations">var NexT=window.NexT||{},CONFIG={hostname:"linqiankun.github.io",root:"/hexoblog/",images:"/hexoblog/images",scheme:"Gemini",version:"8.3.0",exturl:!1,sidebar:{position:"left",display:"post",padding:18,offset:12},copycode:!0,bookmark:{enable:!0,color:"#222",save:"auto"},fancybox:!0,mediumzoom:!1,lazyload:!1,pangu:!0,comments:{style:"tabs",active:null,storage:!0,lazyload:!1,nav:null},motion:{enable:!0,async:!1,transition:{post_block:"fadeIn",post_header:"fadeInDown",post_body:"fadeInDown",coll_header:"fadeInLeft",sidebar:"fadeInUp"}},prism:!1,i18n:{placeholder:"搜索...",empty:"没有找到任何搜索结果：${query}",hits_time:"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）",hits:"找到 ${hits} 个搜索结果"},path:"/hexoblog/search.xml",localsearch:{enable:!0,trigger:"auto",top_n_per_article:1,unescape:!1,preload:!1}}</script><meta name="description" content="1、AOP1.1、概念AOP 为面向切面编程，采用动态代理实现。 1.2、优点采用动态代理的方式，可以增强原有的目标类的方法，我们可以在目标方法执行前后分别做一些事情。对于 aop 就可以在 5 种通知里做一些事情，比如说数据库连接的释放，日志的打印，事务的操作。这种方式，使得不用修改原有程序，就可以增加功能，降低了耦合。 1.3、结构 2、AOP 入门案例2.1、AOP 前AOP 采用动态代理实"><meta property="og:type" content="article"><meta property="og:title" content="Spring AOP入门"><meta property="og:url" content="https://linqiankun.github.io/hexoblog/yuque/weidai/Spring%20AOP%E5%85%A5%E9%97%A8/index.html"><meta property="og:site_name" content="Study"><meta property="og:description" content="1、AOP1.1、概念AOP 为面向切面编程，采用动态代理实现。 1.2、优点采用动态代理的方式，可以增强原有的目标类的方法，我们可以在目标方法执行前后分别做一些事情。对于 aop 就可以在 5 种通知里做一些事情，比如说数据库连接的释放，日志的打印，事务的操作。这种方式，使得不用修改原有程序，就可以增加功能，降低了耦合。 1.3、结构 2、AOP 入门案例2.1、AOP 前AOP 采用动态代理实"><meta property="og:locale" content="zh_CN"><meta property="og:image" content="https://hexoimage.oss-cn-hangzhou.aliyuncs.com/hexobolg/java/springAOP%E7%BB%93%E6%9E%841.png#id=XfoXj&originHeight=290&originWidth=702&originalType=binary%E2%88%B6=1&rotation=0&showTitle=false&status=done&style=none&title=#id=XiJ3z&originHeight=290&originWidth=702&originalType=binary%E2%88%B6=1&rotation=0&showTitle=false&status=done&style=none&title="><meta property="article:published_time" content="2021-03-15T03:19:27.000Z"><meta property="article:modified_time" content="2024-05-14T08:20:04.972Z"><meta property="article:author" content="九分石人"><meta property="article:tag" content="Java"><meta name="twitter:card" content="summary"><meta name="twitter:image" content="https://hexoimage.oss-cn-hangzhou.aliyuncs.com/hexobolg/java/springAOP%E7%BB%93%E6%9E%841.png#id=XfoXj&originHeight=290&originWidth=702&originalType=binary%E2%88%B6=1&rotation=0&showTitle=false&status=done&style=none&title=#id=XiJ3z&originHeight=290&originWidth=702&originalType=binary%E2%88%B6=1&rotation=0&showTitle=false&status=done&style=none&title="><link rel="canonical" href="https://linqiankun.github.io/hexoblog/yuque/weidai/Spring%20AOP%E5%85%A5%E9%97%A8/"><script class="page-configurations">CONFIG.page={sidebar:"",isHome:!1,isPost:!0,lang:"zh-CN"}</script><title>Spring AOP入门 | Study</title><noscript><style>body{margin-top:2rem}.use-motion .collection-header,.use-motion .comments,.use-motion .menu-item,.use-motion .pagination,.use-motion .post-block,.use-motion .post-body,.use-motion .post-header,.use-motion .sidebar{visibility:visible}.use-motion .footer,.use-motion .header,.use-motion .site-brand-container .toggle{opacity:initial}.use-motion .custom-logo-image,.use-motion .site-subtitle,.use-motion .site-title{opacity:initial;top:initial}.use-motion .logo-line{transform:scaleX(1)}.search-pop-overlay,.sidebar-nav{display:none}.sidebar-panel{display:block}</style></noscript><link rel="alternate" href="/hexoblog/atom.xml" title="Study" type="application/atom+xml"><style>.darkmode--activated{--body-bg-color:#282828;--content-bg-color:#333;--card-bg-color:#555;--text-color:#ccc;--blockquote-color:#bbb;--link-color:#ccc;--link-hover-color:#eee;--brand-color:#ddd;--brand-hover-color:#ddd;--table-row-odd-bg-color:#282828;--table-row-hover-bg-color:#363636;--menu-item-bg-color:#555;--btn-default-bg:#222;--btn-default-color:#ccc;--btn-default-border-color:#555;--btn-default-hover-bg:#666;--btn-default-hover-color:#ccc;--btn-default-hover-border-color:#666;--highlight-background:#282b2e;--highlight-foreground:#a9b7c6;--highlight-gutter-background:#34393d;--highlight-gutter-foreground:#9ca9b6}.darkmode--activated img{opacity:.75}.darkmode--activated img:hover{opacity:.9}.darkmode--activated code{color:#69dbdc;background:0 0}button.darkmode-toggle{z-index:9999}.darkmode-ignore,img{display:flex!important}.beian img{display:inline-block!important}</style></head><body itemscope itemtype="http://schema.org/WebPage" class="use-motion"><div class="headband"></div><main class="main"><header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="header-inner"><div class="site-brand-container"><div class="site-nav-toggle"><div class="toggle" aria-label="切换导航栏" role="button"><span class="toggle-line"></span> <span class="toggle-line"></span> <span class="toggle-line"></span></div></div><div class="site-meta"><a href="/hexoblog/" class="brand" rel="start"><i class="logo-line"></i><h1 class="site-title">Study</h1><i class="logo-line"></i></a><p class="site-subtitle" itemprop="description">好好学习，天天向上！</p></div><div class="site-nav-right"><div class="toggle popup-trigger"><i class="fa fa-search fa-fw fa-lg"></i></div></div></div><nav class="site-nav"><ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/hexoblog/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-about"><a href="/hexoblog/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-tags"><a href="/hexoblog/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/hexoblog/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/hexoblog/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li><li class="menu-item menu-item-sitemap"><a href="/hexoblog/sitemap.xml" rel="section"><i class="fa fa-sitemap fa-fw"></i>站点地图</a></li><li class="menu-item menu-item-search"><a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索</a></li></ul></nav><div class="search-pop-overlay"><div class="popup search-popup"><div class="search-header"><span class="search-icon"><i class="fa fa-search"></i></span><div class="search-input-container"><input autocomplete="off" autocapitalize="off" maxlength="80" placeholder="搜索..." spellcheck="false" type="search" class="search-input"></div><span class="popup-btn-close" role="button"><i class="fa fa-times-circle"></i></span></div><div class="search-result-container no-result"><div class="search-result-icon"><i class="fa fa-spinner fa-pulse fa-5x"></i></div></div></div></div></div><div class="toggle sidebar-toggle" role="button"><span class="toggle-line"></span> <span class="toggle-line"></span> <span class="toggle-line"></span></div><aside class="sidebar"><div class="sidebar-inner sidebar-nav-active sidebar-toc-active"><ul class="sidebar-nav"><li class="sidebar-nav-toc">文章目录</li><li class="sidebar-nav-overview">站点概览</li></ul><div class="sidebar-panel-container"><div class="post-toc-wrap sidebar-panel"><div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#1%E3%80%81AOP"><span class="nav-number">1.</span> <span class="nav-text">1、AOP</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-1%E3%80%81%E6%A6%82%E5%BF%B5"><span class="nav-number">1.1.</span> <span class="nav-text">1.1、概念</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-2%E3%80%81%E4%BC%98%E7%82%B9"><span class="nav-number">1.2.</span> <span class="nav-text">1.2、优点</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-3%E3%80%81%E7%BB%93%E6%9E%84"><span class="nav-number">1.3.</span> <span class="nav-text">1.3、结构</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#2%E3%80%81AOP-%E5%85%A5%E9%97%A8%E6%A1%88%E4%BE%8B"><span class="nav-number">2.</span> <span class="nav-text">2、AOP 入门案例</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#2-1%E3%80%81AOP-%E5%89%8D"><span class="nav-number">2.1.</span> <span class="nav-text">2.1、AOP 前</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-1%E3%80%81%E4%BE%9D%E8%B5%96%E5%8C%85"><span class="nav-number">2.1.1.</span> <span class="nav-text">2.1.1、依赖包</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-2%E3%80%81jdk-%E5%8A%A8%E6%80%81%E4%BB%A3%E7%90%86-AOP-%E6%89%8B%E5%8A%A8%E6%96%B9%E5%BC%8F"><span class="nav-number">2.2.</span> <span class="nav-text">2.2、jdk 动态代理(AOP 手动方式)</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-1%E3%80%81%E7%9B%AE%E6%A0%87%E7%B1%BB%EF%BC%9A%E6%8E%A5%E5%8F%A3-%E5%AE%9E%E7%8E%B0%E7%B1%BB"><span class="nav-number">2.2.1.</span> <span class="nav-text">2.2.1、目标类：接口+实现类</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-2%E3%80%81%E5%88%87%E9%9D%A2%E7%B1%BB%EF%BC%9A%E7%94%A8%E4%BA%8E%E4%BF%9D%E5%AD%98%E9%80%9A%E7%9F%A5"><span class="nav-number">2.2.2.</span> <span class="nav-text">2.2.2、切面类：用于保存通知</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-3%E3%80%81%E4%BB%A3%E7%90%86%E7%B1%BB%EF%BC%9A%E7%94%9F%E6%88%90%E4%BB%A3%E7%90%86%E5%AF%B9%E8%B1%A1"><span class="nav-number">2.2.3.</span> <span class="nav-text">2.2.3、代理类：生成代理对象</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-4%E3%80%81%E6%B5%8B%E8%AF%95%E7%B1%BB%EF%BC%9A"><span class="nav-number">2.2.4.</span> <span class="nav-text">2.2.4、测试类：</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-3%E3%80%81cglib-%E4%BB%A3%E7%90%86%E6%94%BE%E5%BC%8F"><span class="nav-number">2.3.</span> <span class="nav-text">2.3、cglib 代理放式</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-1%E3%80%81%E7%9B%AE%E6%A0%87%E7%B1%BB%E7%AD%89%E5%90%8C%E4%BA%8E-jdk-%E5%8A%A8%E6%80%81%E4%BB%A3%E7%90%86%E7%9A%84%E6%8E%A5%E5%8F%A3%E7%9A%84%E5%AE%9E%E7%8E%B0%E7%B1%BB"><span class="nav-number">2.3.1.</span> <span class="nav-text">2.3.1、目标类等同于 jdk 动态代理的接口的实现类</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-2%E3%80%81%E5%88%87%E9%9D%A2%E7%B1%BB%E5%90%8C%E4%B8%8A"><span class="nav-number">2.3.2.</span> <span class="nav-text">2.3.2、切面类同上</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-3%E3%80%81%E6%B5%8B%E8%AF%95%E7%B1%BB%E5%90%8C%E4%B8%8A"><span class="nav-number">2.3.3.</span> <span class="nav-text">2.3.3、测试类同上</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-4%E3%80%81%E4%BB%A3%E7%90%86%E7%B1%BB"><span class="nav-number">2.3.4.</span> <span class="nav-text">2.3.4、代理类</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-4%E3%80%81AOP-%E5%8D%8A%E8%87%AA%E5%8A%A8%E4%BB%A3%E7%90%86"><span class="nav-number">2.4.</span> <span class="nav-text">2.4、AOP 半自动代理</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-4-1%E3%80%81%E7%9B%AE%E6%A0%87%E7%B1%BB%E4%B8%8E-jdk-%E5%8A%A8%E6%80%81%E4%BB%A3%E7%90%86%E7%9B%B8%E5%90%8C"><span class="nav-number">2.4.1.</span> <span class="nav-text">2.4.1、目标类与 jdk 动态代理相同</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-4-2%E3%80%81%E5%88%87%E9%9D%A2%E7%B1%BB%EF%BC%9A%E7%8E%AF%E7%BB%95%E9%80%9A%E7%9F%A5%EF%BC%88%E6%AD%A4%E5%A4%84%E4%B8%8E%E5%85%B6%E4%BB%96%E4%B8%8D%E5%90%8C%EF%BC%89"><span class="nav-number">2.4.2.</span> <span class="nav-text">2.4.2、切面类：环绕通知（此处与其他不同）</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-4-3%E3%80%81%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6-beans-xml"><span class="nav-number">2.4.3.</span> <span class="nav-text">2.4.3、配置文件 beans.xml</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-4-4%E3%80%81%E6%B5%8B%E8%AF%95%E7%B1%BB"><span class="nav-number">2.4.4.</span> <span class="nav-text">2.4.4、测试类</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-5%E3%80%81AOP-%E5%85%A8%E8%87%AA%E5%8A%A8%E4%BB%A3%E7%90%86"><span class="nav-number">2.5.</span> <span class="nav-text">2.5、AOP 全自动代理</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-5-1%E3%80%81%E7%9B%AE%E6%A0%87%E7%B1%BB%E4%B8%8E-jdk-%E5%8A%A8%E6%80%81%E4%BB%A3%E7%90%86%E7%9B%B8%E5%90%8C"><span class="nav-number">2.5.1.</span> <span class="nav-text">2.5.1、目标类与 jdk 动态代理相同</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-5-2%E3%80%81%E6%B5%8B%E8%AF%95%E7%B1%BB%E4%B8%8E%E5%8D%8A%E8%87%AA%E5%8A%A8%E6%96%B9%E5%BC%8F%E7%9B%B8%E5%90%8C"><span class="nav-number">2.5.2.</span> <span class="nav-text">2.5.2、测试类与半自动方式相同</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-5-3%E3%80%81%E5%88%87%E9%9D%A2%E7%B1%BB"><span class="nav-number">2.5.3.</span> <span class="nav-text">2.5.3、切面类</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-5-4%E3%80%81%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6-beans-xml"><span class="nav-number">2.5.4.</span> <span class="nav-text">2.5.4、配置文件 beans.xml</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-5-5%E3%80%81%E7%BB%93%E6%9E%9C"><span class="nav-number">2.5.5.</span> <span class="nav-text">2.5.5、结果</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-5-5-1%E3%80%81%E9%85%8D%E7%BD%AE%E7%8E%AF%E7%BB%95%E9%80%9A%E7%9F%A5%E6%97%B6"><span class="nav-number">2.5.5.1.</span> <span class="nav-text">2.5.5.1、配置环绕通知时</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-5-5-2%E3%80%81%E4%B8%8D%E9%85%8D%E7%BD%AE%E7%8E%AF%E7%BB%95%E9%80%9A%E7%9F%A5"><span class="nav-number">2.5.5.2.</span> <span class="nav-text">2.5.5.2、不配置环绕通知</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-5-5-3%E3%80%81%E5%85%B6%E4%BB%96"><span class="nav-number">2.5.5.3.</span> <span class="nav-text">2.5.5.3、其他</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-5-6%E3%80%81%E8%A1%A5%E5%85%85"><span class="nav-number">2.5.6.</span> <span class="nav-text">2.5.6、补充</span></a></li></ol></li></ol></li></ol></div></div><div class="site-overview-wrap sidebar-panel"><div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person"><img class="site-author-image" itemprop="image" alt="九分石人" src="/hexoblog/images/avatar.png"><p class="site-author-name" itemprop="name">九分石人</p><div class="site-description" itemprop="description">my blogs</div></div><div class="site-state-wrap site-overview-item animated"><nav class="site-state"><div class="site-state-item site-state-posts"><a href="/hexoblog/archives/"><span class="site-state-item-count">239</span> <span class="site-state-item-name">日志</span></a></div><div class="site-state-item site-state-categories"><a href="/hexoblog/categories/"><span class="site-state-item-count">35</span> <span class="site-state-item-name">分类</span></a></div><div class="site-state-item site-state-tags"><a href="/hexoblog/tags/"><span class="site-state-item-count">64</span> <span class="site-state-item-name">标签</span></a></div></nav></div><div class="links-of-author site-overview-item animated"><span class="links-of-author-item"><a href="https://github.com/linqiankun" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;linqiankun" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a> </span><span class="links-of-author-item"><a href="linqiankun:191580378@qq.com" title="E-Mail → linqiankun:191580378@qq.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a> </span><span class="links-of-author-item"><a href="https://weibo.com/jiufenshiren" title="Weibo → https:&#x2F;&#x2F;weibo.com&#x2F;jiufenshiren" rel="noopener" target="_blank"><i class="fab fa-weibo fa-fw"></i>Weibo</a> </span><span class="links-of-author-item"><a href="/hexoblog/atom.xml" title="Rss → &#x2F;atom.xml"><i class="fa fa-rss fa-fw"></i>Rss</a></span></div><div class="cc-license site-overview-item animated" itemprop="license"><a href="https://creativecommons.org/licenses/by-nc-sa/4.0/zh-CN" class="cc-opacity" rel="noopener" target="_blank"><img src="/hexoblog/images/cc-by-nc-sa.svg" alt="Creative Commons"></a></div><div class="links-of-blogroll site-overview-item animated"><div class="links-of-blogroll-title"><i class="fa fa-globe fa-fw"></i> Links</div><ul class="links-of-blogroll-list"><li class="links-of-blogroll-item"><a href="http://124.222.164.140:3001/workspace/hexoblog" title="http:&#x2F;&#x2F;124.222.164.140:3001&#x2F;workspace&#x2F;hexoblog" rel="noopener" target="_blank">AnythingLLM</a></li><li class="links-of-blogroll-item"><a href="https://www.yuque.com/jiufenshiren" title="https:&#x2F;&#x2F;www.yuque.com&#x2F;jiufenshiren" rel="noopener" target="_blank">YuQue</a></li><li class="links-of-blogroll-item"><a href="https://github.com/LinQiankun" title="https:&#x2F;&#x2F;github.com&#x2F;LinQiankun" rel="noopener" target="_blank">Github</a></li><li class="links-of-blogroll-item"><a href="https://gitee.com/linqiankun" title="https:&#x2F;&#x2F;gitee.com&#x2F;linqiankun" rel="noopener" target="_blank">Gitee</a></li><li class="links-of-blogroll-item"><a href="https://my.oschina.net/linqiankun" title="https:&#x2F;&#x2F;my.oschina.net&#x2F;linqiankun" rel="noopener" target="_blank">OsChina</a></li><li class="links-of-blogroll-item"><a href="https://segmentfault.com/u/jiufenshiren" title="https:&#x2F;&#x2F;segmentfault.com&#x2F;u&#x2F;jiufenshiren" rel="noopener" target="_blank">Segmentfault</a></li><li class="links-of-blogroll-item"><a href="https://juejin.cn/user/3263006243306205" title="https:&#x2F;&#x2F;juejin.cn&#x2F;user&#x2F;3263006243306205" rel="noopener" target="_blank">Juejin</a></li><li class="links-of-blogroll-item"><a href="https://blog.csdn.net/qq_36985536?spm=1010.2135.3001.5421" title="https:&#x2F;&#x2F;blog.csdn.net&#x2F;qq_36985536?spm&#x3D;1010.2135.3001.5421" rel="noopener" target="_blank">CSDN</a></li><li class="links-of-blogroll-item"><a href="https://www.cnblogs.com/jiufenshiren" title="https:&#x2F;&#x2F;www.cnblogs.com&#x2F;jiufenshiren" rel="noopener" target="_blank">CnBlogs</a></li><li class="links-of-blogroll-item"><a href="https://home.51cto.com/space?uid=12848310" title="https:&#x2F;&#x2F;home.51cto.com&#x2F;space?uid&#x3D;12848310" rel="noopener" target="_blank">51CTO</a></li></ul></div><script data-embed-id="94f790d6-f7ac-4d04-ad53-55d841525b5a" data-base-api-url="http://124.222.164.140:3001/api/embed" src="http://124.222.164.140:3001/embed/anythingllm-chat-widget.min.js"></script></div></div></div></aside><div class="sidebar-dimmer"></div></header><div class="back-to-top" role="button" aria-label="返回顶部"><i class="fa fa-arrow-up"></i> <span>0%</span></div><a role="button" class="book-mark-link book-mark-link-fixed"></a><noscript><div class="noscript-warning">Theme NexT works best with JavaScript enabled</div></noscript><div class="main-inner post posts-expand"><div class="post-block"><article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN"><link itemprop="mainEntityOfPage" href="https://linqiankun.github.io/hexoblog/yuque/weidai/Spring%20AOP%E5%85%A5%E9%97%A8/"><span hidden itemprop="author" itemscope itemtype="http://schema.org/Person"><meta itemprop="image" content="/hexoblog/images/avatar.png"><meta itemprop="name" content="九分石人"><meta itemprop="description" content="my blogs"></span><span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization"><meta itemprop="name" content="Study"></span><header class="post-header"><h1 class="post-title" itemprop="name headline">Spring AOP入门</h1><div class="post-meta-container"><div class="post-meta"><span class="post-meta-item"><span class="post-meta-item-icon"><i class="far fa-calendar"></i> </span><span class="post-meta-item-text">发表于</span> <time title="创建时间：2021-03-15 11:19:27 11:19:27" itemprop="dateCreated datePublished" datetime="2021-03-15T11:19:27+08:00">2021-03-15 11:19:27</time> </span><span class="post-meta-item"><span class="post-meta-item-icon"><i class="far fa-calendar-check"></i> </span><span class="post-meta-item-text">更新于</span> <time title="修改时间：2024-05-14 16:20:04 16:20:04" itemprop="dateModified" datetime="2024-05-14T16:20:04+08:00">2024-05-14 16:20:04</time> </span><span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display:none"><span class="post-meta-item-icon"><i class="far fa-eye"></i> </span><span class="post-meta-item-text">阅读次数：</span> <span id="busuanzi_value_page_pv"></span></span></div><div class="post-meta"><span class="post-meta-item" title="本文字数"><span class="post-meta-item-icon"><i class="far fa-file-word"></i> </span><span class="post-meta-item-text">本文字数：</span> <span>11k</span> </span><span class="post-meta-item" title="阅读时长"><span class="post-meta-item-icon"><i class="far fa-clock"></i> </span><span class="post-meta-item-text">阅读时长 &asymp;</span> <span>10 分钟</span></span></div></div></header><div class="post-body" itemprop="articleBody"><h1 id="1、AOP"><a href="#1、AOP" class="headerlink" title="1、AOP"></a>1、AOP</h1><h2 id="1-1、概念"><a href="#1-1、概念" class="headerlink" title="1.1、概念"></a>1.1、概念</h2><p>AOP 为面向切面编程，采用动态代理实现。</p><h2 id="1-2、优点"><a href="#1-2、优点" class="headerlink" title="1.2、优点"></a>1.2、优点</h2><p>采用动态代理的方式，可以增强原有的目标类的方法，我们可以在目标方法执行前后分别做一些事情。<br>对于 aop 就可以在 5 种通知里做一些事情，比如说数据库连接的释放，日志的打印，事务的操作。<br>这种方式，使得不用修改原有程序，就可以增加功能，降低了耦合。</p><h2 id="1-3、结构"><a href="#1-3、结构" class="headerlink" title="1.3、结构"></a>1.3、结构</h2><p><img src="https://hexoimage.oss-cn-hangzhou.aliyuncs.com/hexobolg/java/springAOP%E7%BB%93%E6%9E%841.png#id=XfoXj&originHeight=290&originWidth=702&originalType=binary%E2%88%B6=1&rotation=0&showTitle=false&status=done&style=none&title=#id=XiJ3z&originHeight=290&originWidth=702&originalType=binary%E2%88%B6=1&rotation=0&showTitle=false&status=done&style=none&title="></p><h1 id="2、AOP-入门案例"><a href="#2、AOP-入门案例" class="headerlink" title="2、AOP 入门案例"></a>2、AOP 入门案例</h1><h2 id="2-1、AOP-前"><a href="#2-1、AOP-前" class="headerlink" title="2.1、AOP 前"></a>2.1、AOP 前</h2><p>AOP 采用动态代理实现，故很有必要在看 AOP 前先看动态代理的实现。动态代理分为两种，对于有接口，有实现类的可以采用 jdk 动态代理，对于没有接口，只有实现类的，需要采用 cglib 代理。</p><h3 id="2-1-1、依赖包"><a href="#2-1-1、依赖包" class="headerlink" title="2.1.1、依赖包"></a>2.1.1、依赖包</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>org.springframework<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>spring-core<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.2.7.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>org.springframework<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>spring-context<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.2.7.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>org.springframework<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>spring-beans<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.2.7.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>org.springframework<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>spring-expression<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.2.7.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>org.springframework<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>spring-aop<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.2.7.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.aopalliance<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>com.springsource.org.aopalliance<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.aspectj<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>aspectjweaver<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.6.12<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>commons-logging<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>commons-logging<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.1<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure><h2 id="2-2、jdk-动态代理-AOP-手动方式"><a href="#2-2、jdk-动态代理-AOP-手动方式" class="headerlink" title="2.2、jdk 动态代理(AOP 手动方式)"></a>2.2、jdk 动态代理(AOP 手动方式)</h2><p>jdk 动态代理中，代理类需要实现目标类的所有接口。</p><h3 id="2-2-1、目标类：接口-实现类"><a href="#2-2-1、目标类：接口-实现类" class="headerlink" title="2.2.1、目标类：接口+实现类"></a>2.2.1、目标类：接口+实现类</h3><p>接口：</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">UserService</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">addUser</span><span class="params">()</span></span>;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">updateUser</span><span class="params">()</span></span>;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">deleteUser</span><span class="params">()</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>实现类：</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserServiceImpl</span> <span class="keyword">implements</span> <span class="title">UserService</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">addUser</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;addUser&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">updateUser</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;updateUser&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">deleteUser</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;deleteUser&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h3 id="2-2-2、切面类：用于保存通知"><a href="#2-2-2、切面类：用于保存通知" class="headerlink" title="2.2.2、切面类：用于保存通知"></a>2.2.2、切面类：用于保存通知</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyAspect</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">before</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;brfore&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">after</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;after&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h3 id="2-2-3、代理类：生成代理对象"><a href="#2-2-3、代理类：生成代理对象" class="headerlink" title="2.2.3、代理类：生成代理对象"></a>2.2.3、代理类：生成代理对象</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyBeanFactory</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> UserService <span class="title">createService</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 1、目标类</span></span><br><span class="line">        <span class="keyword">final</span> UserService userService = <span class="keyword">new</span> UserServiceImpl();</span><br><span class="line">        <span class="comment">// 2、切面类</span></span><br><span class="line">        <span class="keyword">final</span> MyAspect myAspect = <span class="keyword">new</span> MyAspect();</span><br><span class="line">        <span class="comment">// 3、代理对象</span></span><br><span class="line">        <span class="comment">// 参数1、类加载器</span></span><br><span class="line">                         当前类.class.getClassLoader()</span><br><span class="line">                        目标类对象.getClass().getClassLoader()</span><br><span class="line">        <span class="comment">// 参数2、目标类所实现的所有接口</span></span><br><span class="line">                        目标类对象.getClass().getInterfaces()</span><br><span class="line">                        <span class="keyword">new</span> Class[]&#123;接口.class&#125;</span><br><span class="line">        <span class="comment">// 参数3、InvocationHandler处理类，采用匿名内部类实现</span></span><br><span class="line">        UserService proxyService = (UserService) Proxy.newProxyInstance(MyBeanFactory.class.getClassLoader(),</span><br><span class="line">        userService.getClass().getInterfaces(), <span class="keyword">new</span> InvocationHandler() &#123;</span><br><span class="line">            <span class="comment">//参数1proxy、代理对象</span></span><br><span class="line">            <span class="comment">//参数2method、要执行方法的描述对象</span></span><br><span class="line">            <span class="comment">//参数3arg2、方法的实际参数</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> Object <span class="title">invoke</span><span class="params">(Object proxy, Method method, Object[] arg2)</span> <span class="keyword">throws</span> Throwable </span>&#123;</span><br><span class="line">                myAspect.before();</span><br><span class="line">                Object obj = method.invoke(userService, arg2);</span><br><span class="line">                myAspect.after();</span><br><span class="line">                <span class="keyword">return</span> obj;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">        <span class="keyword">return</span> proxyService;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h3 id="2-2-4、测试类："><a href="#2-2-4、测试类：" class="headerlink" title="2.2.4、测试类："></a>2.2.4、测试类：</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">fun</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    UserService userService = MyBeanFactory.createService();</span><br><span class="line">    userService.addUser();</span><br><span class="line">    userService.updateUser();</span><br><span class="line">    userService.deleteUser();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h2 id="2-3、cglib-代理放式"><a href="#2-3、cglib-代理放式" class="headerlink" title="2.3、cglib 代理放式"></a>2.3、cglib 代理放式</h2><p>cglib 代理适用于没有接口只有实现类的情况。cglib 是通过让代理类继承目标类，从而增强目标类的方法。</p><h3 id="2-3-1、目标类等同于-jdk-动态代理的接口的实现类"><a href="#2-3-1、目标类等同于-jdk-动态代理的接口的实现类" class="headerlink" title="2.3.1、目标类等同于 jdk 动态代理的接口的实现类"></a>2.3.1、目标类等同于 jdk 动态代理的接口的实现类</h3><h3 id="2-3-2、切面类同上"><a href="#2-3-2、切面类同上" class="headerlink" title="2.3.2、切面类同上"></a>2.3.2、切面类同上</h3><h3 id="2-3-3、测试类同上"><a href="#2-3-3、测试类同上" class="headerlink" title="2.3.3、测试类同上"></a>2.3.3、测试类同上</h3><h3 id="2-3-4、代理类"><a href="#2-3-4、代理类" class="headerlink" title="2.3.4、代理类"></a>2.3.4、代理类</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyBeanFactory</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> UserServiceImpl <span class="title">createService</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 1、目标类</span></span><br><span class="line">    <span class="keyword">final</span> UserServiceImpl userService = <span class="keyword">new</span> UserServiceImpl();</span><br><span class="line">    <span class="comment">// 2、切面类</span></span><br><span class="line">    <span class="keyword">final</span> MyAspect myAspect = <span class="keyword">new</span> MyAspect();</span><br><span class="line">    <span class="comment">// 3、代理类</span></span><br><span class="line">    Enhancer enhancer = <span class="keyword">new</span> Enhancer();</span><br><span class="line">    <span class="comment">//设置父类</span></span><br><span class="line">    enhancer.setSuperclass(userService.getClass());</span><br><span class="line">    <span class="comment">/* 设置回调函数 , MethodInterceptor接口 等效 jdk中 InvocationHandler接口</span></span><br><span class="line"><span class="comment">     * intercept() 等效 jdk invoke()</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    enhancer.setCallback(<span class="keyword">new</span> MethodInterceptor() &#123;</span><br><span class="line">        <span class="comment">//参数1、代理对象</span></span><br><span class="line">        <span class="comment">//参数2、要执行方法的描述对象(当前)</span></span><br><span class="line">        <span class="comment">//参数3、方法的实际参数</span></span><br><span class="line">        <span class="comment">//参数4、代理类方法的代理</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> Object <span class="title">intercept</span><span class="params">(Object arg0, Method arg1, Object[] arg2, MethodProxy arg3)</span> <span class="keyword">throws</span> Throwable </span>&#123;</span><br><span class="line">            myAspect.before();</span><br><span class="line">            <span class="comment">//通过反射的方式执行要目标类的方法</span></span><br><span class="line">            Object obj = arg1.invoke(userService, arg2);</span><br><span class="line">            <span class="comment">//执行代理类的父类（目标类）</span></span><br><span class="line">            arg3.invokeSuper(arg0, arg2);</span><br><span class="line">            myAspect.after();</span><br><span class="line">            <span class="keyword">return</span> obj;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="comment">//创建代理对象，向上转型</span></span><br><span class="line">    UserServiceImpl proxyService = (UserServiceImpl) enhancer.create();</span><br><span class="line">    <span class="keyword">return</span> proxyService;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h2 id="2-4、AOP-半自动代理"><a href="#2-4、AOP-半自动代理" class="headerlink" title="2.4、AOP 半自动代理"></a>2.4、AOP 半自动代理</h2><p>半自动代理采用 spring 提供的 org.springframework.aop.framework.ProxyFactoryBean 类（一个代理工厂）生成代理对象</p><h3 id="2-4-1、目标类与-jdk-动态代理相同"><a href="#2-4-1、目标类与-jdk-动态代理相同" class="headerlink" title="2.4.1、目标类与 jdk 动态代理相同"></a>2.4.1、目标类与 jdk 动态代理相同</h3><h3 id="2-4-2、切面类：环绕通知（此处与其他不同）"><a href="#2-4-2、切面类：环绕通知（此处与其他不同）" class="headerlink" title="2.4.2、切面类：环绕通知（此处与其他不同）"></a>2.4.2、切面类：环绕通知（此处与其他不同）</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyAspect</span> <span class="keyword">implements</span> <span class="title">MethodInterceptor</span></span></span><br><span class="line"><span class="class">    <span class="title">public</span> <span class="title">Object</span> <span class="title">invoke</span>(<span class="title">MethodInvocation</span> <span class="title">arg0</span>) <span class="title">throws</span> <span class="title">Throwable</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;before&quot;</span>);</span><br><span class="line">        Object obj = arg0.proceed();</span><br><span class="line">        System.out.println(<span class="string">&quot;after&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> obj;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h3 id="2-4-3、配置文件-beans-xml"><a href="#2-4-3、配置文件-beans-xml" class="headerlink" title="2.4.3、配置文件 beans.xml"></a>2.4.3、配置文件 beans.xml</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">beans</span> <span class="attr">xmlns</span>=<span class="string">&quot;http://www.springframework.org/schema/beans&quot;</span></span></span><br><span class="line"><span class="tag">             <span class="attr">xmlns:xsi</span>=<span class="string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span></span><br><span class="line"><span class="tag">             <span class="attr">xsi:schemaLocation</span>=<span class="string">&quot;http://www.springframework.org/schema/beans</span></span></span><br><span class="line"><span class="tag"><span class="string">                                                http://www.springframework.org/schema/beans/spring-beans.xsd&quot;</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 目标类 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;userService&quot;</span> <span class="attr">class</span>=<span class="string">&quot;beanfactory.UserServiceImpl&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 切面类 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;myAspect&quot;</span> <span class="attr">class</span>=<span class="string">&quot;beanfactory.MyAspect&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 代理类 --&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 使用spring提供的代理工厂 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;proxyService&quot;</span> <span class="attr">class</span>=<span class="string">&quot;org.springframework.aop.framework.ProxyFactoryBean&quot;</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 目标类的接口 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;interfaces&quot;</span> <span class="attr">value</span>=<span class="string">&quot;beanfactory.UserService&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 目标类 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;target&quot;</span> <span class="attr">ref</span>=<span class="string">&quot;userService&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 只能放一个切面类 --&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 切面类的名称 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;interceptorNames&quot;</span> <span class="attr">value</span>=<span class="string">&quot;myAspect&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">beans</span>&gt;</span></span><br></pre></td></tr></table></figure><h3 id="2-4-4、测试类"><a href="#2-4-4、测试类" class="headerlink" title="2.4.4、测试类"></a>2.4.4、测试类</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">fun</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    String xmlPath = <span class="string">&quot;beanfactory/beans.xml&quot;</span>;</span><br><span class="line">    ApplicationContext applicationContext = <span class="keyword">new</span> ClassPathXmlApplicationContext(xmlPath);</span><br><span class="line">    <span class="comment">// 获得代理对象</span></span><br><span class="line">    UserService userService = (UserService) applicationContext.getBean(<span class="string">&quot;proxyService&quot;</span>);</span><br><span class="line">    userService.addUser();</span><br><span class="line">    userService.updateUser();</span><br><span class="line">    userService.deleteUser();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>2.4.5、结果<br>before<br>addUser<br>after<br>before<br>updateUser<br>after<br>before<br>deleteUser<br>after</p><h2 id="2-5、AOP-全自动代理"><a href="#2-5、AOP-全自动代理" class="headerlink" title="2.5、AOP 全自动代理"></a>2.5、AOP 全自动代理</h2><p>全自动代理仅需要在 beans.xml 中配置一下就好，使用<a href="aop:config">aop:config</a>。<br>spring 会自动判断使用那种代理方式，有接口时采用 jdk 方式，没有接口时采用 cglib 方式。</p><h3 id="2-5-1、目标类与-jdk-动态代理相同"><a href="#2-5-1、目标类与-jdk-动态代理相同" class="headerlink" title="2.5.1、目标类与 jdk 动态代理相同"></a>2.5.1、目标类与 jdk 动态代理相同</h3><h3 id="2-5-2、测试类与半自动方式相同"><a href="#2-5-2、测试类与半自动方式相同" class="headerlink" title="2.5.2、测试类与半自动方式相同"></a>2.5.2、测试类与半自动方式相同</h3><h3 id="2-5-3、切面类"><a href="#2-5-3、切面类" class="headerlink" title="2.5.3、切面类"></a>2.5.3、切面类</h3><p>AOP 提供了 5 中通知类型，分别为，前置通知，返回通知，异常通知，最终通知，环绕通知，其中环绕通知最为强大。<br>除环绕通知，其他可以没有参数。</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyAspect</span> </span>&#123;</span><br><span class="line">    <span class="comment">//前置通知，在目标类方法执行前执行</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">before</span><span class="params">(JoinPoint joinPoint)</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;before&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//返回通知，在目标类方法执行后执行，可以拿到目标类方法的返回值，如果没有就是null</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">afterreturning</span><span class="params">(JoinPoint joinPoint, Object ret)</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;afterreturning&quot;</span> + ret);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//异常通知，当目标类方法抛出异常时执行，可以拿到异常信息，类似于在catch块里面的方法</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">throwable</span><span class="params">(JoinPoint joinPoint, Throwable e)</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;throwable&quot;</span> + <span class="string">&quot; &quot;</span> + e.getMessage());</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//在最后执行</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">afterafter</span><span class="params">(JoinPoint joinPoint)</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;afterafter&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//在方法前后都会执行</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">invoke</span><span class="params">(ProceedingJoinPoint jontPoint)</span> <span class="keyword">throws</span> Throwable </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;环绕通知--------&gt;前&quot;</span>);</span><br><span class="line">        Object obj = jontPoint.proceed();</span><br><span class="line">        System.out.println(<span class="string">&quot;环绕通知----------&gt;后&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> obj;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h3 id="2-5-4、配置文件-beans-xml"><a href="#2-5-4、配置文件-beans-xml" class="headerlink" title="2.5.4、配置文件 beans.xml"></a>2.5.4、配置文件 beans.xml</h3><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=<span class="string">&quot;1.0&quot;</span> encoding=<span class="string">&quot;UTF-8&quot;</span>?&gt;</span><br><span class="line">&lt;beans xmlns=<span class="string">&quot;http://www.springframework.org/schema/beans&quot;</span></span><br><span class="line">             xmlns:xsi=<span class="string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span><br><span class="line">             xmlns:aop=<span class="string">&quot;http://www.springframework.org/schema/aop&quot;</span></span><br><span class="line">             xsi:schemaLocation=<span class="string">&quot;http://www.springframework.org/schema/beans</span></span><br><span class="line"><span class="string">                                                http://www.springframework.org/schema/beans/spring-beans.xsd</span></span><br><span class="line"><span class="string">                                                http://www.springframework.org/schema/aop</span></span><br><span class="line"><span class="string">                                                http://www.springframework.org/schema/aop/spring-aop.xsd&quot;</span>&gt;</span><br><span class="line">    &lt;!-- 目标类 --&gt;</span><br><span class="line">    &lt;bean id=&quot;userService&quot; class=&quot;aop.UserServiceImpl&quot;&gt;&lt;/bean&gt;</span><br><span class="line">    &lt;!-- 切面类 --&gt;</span><br><span class="line">    &lt;bean id=&quot;myAspect&quot; class=&quot;aop.MyAspect&quot;&gt;&lt;/bean&gt;</span><br><span class="line">    &lt;!-- proxs-target-<span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;true&quot;</span>：强制采用cglib方式代理 --&gt;</span><br><span class="line">    &lt;aop:config proxy-target-<span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;true&quot;</span>&gt;</span><br><span class="line">        &lt;!-- 切入点 --&gt;</span><br><span class="line">        &lt;aop:pointcut expression=<span class="string">&quot;execution(* aop.UserService.*(..))&quot;</span> id=<span class="string">&quot;myPointCut&quot;</span> /&gt;</span><br><span class="line">        &lt;!-- 切面 --&gt;</span><br><span class="line">        &lt;aop:aspect ref=<span class="string">&quot;myAspect&quot;</span>&gt;</span><br><span class="line">            &lt;!-- 前置通知 --&gt;</span><br><span class="line">            &lt;aop:before method=<span class="string">&quot;before&quot;</span> pointcut-ref=<span class="string">&quot;myPointCut&quot;</span> /&gt;</span><br><span class="line">            &lt;!-- 返回通知 --&gt;</span><br><span class="line">            &lt;aop:after-returning method=<span class="string">&quot;afterreturning&quot;</span> returning=<span class="string">&quot;ret&quot;</span> pointcut-ref=<span class="string">&quot;myPointCut&quot;</span> /&gt;</span><br><span class="line">            &lt;!-- 环绕通知 --&gt;</span><br><span class="line">            &lt;aop:around method=<span class="string">&quot;invoke&quot;</span> pointcut-ref=<span class="string">&quot;myPointCut&quot;</span> /&gt;</span><br><span class="line">            &lt;!-- 异常通知 --&gt;</span><br><span class="line">            &lt;aop:after-throwing method=<span class="string">&quot;throwable&quot;</span> throwing=<span class="string">&quot;e&quot;</span> pointcut-ref=<span class="string">&quot;myPointCut&quot;</span> /&gt;</span><br><span class="line">            &lt;!-- 最终通知 --&gt;</span><br><span class="line">            &lt;aop:after method=<span class="string">&quot;afterafter&quot;</span> pointcut-ref=<span class="string">&quot;myPointCut&quot;</span> /&gt;</span><br><span class="line">        &lt;/aop:aspect&gt;</span><br><span class="line">    &lt;/aop:config&gt;</span><br><span class="line">&lt;/beans&gt;</span><br></pre></td></tr></table></figure><h3 id="2-5-5、结果"><a href="#2-5-5、结果" class="headerlink" title="2.5.5、结果"></a>2.5.5、结果</h3><p>因为环绕通知中有 return 语句，故执行顺序和配置有关。</p><h4 id="2-5-5-1、配置环绕通知时"><a href="#2-5-5-1、配置环绕通知时" class="headerlink" title="2.5.5.1、配置环绕通知时"></a>2.5.5.1、配置环绕通知时</h4><p>before 环绕通知——–&gt;前 addUser<br>afterafter 环绕通知———-&gt;后 afterreturning1</p><hr><p>before 环绕通知——–&gt;前 deleteUser<br>afterafter 环绕通知———-&gt;后 afterreturningnull</p><hr><p>before 环绕通知——–&gt;前 updateUser<br>afterafter<br>throwable / by zero</p><h4 id="2-5-5-2、不配置环绕通知"><a href="#2-5-5-2、不配置环绕通知" class="headerlink" title="2.5.5.2、不配置环绕通知"></a>2.5.5.2、不配置环绕通知</h4><p>before<br>addUser<br>afterreturning1<br>afterafter</p><hr><p>before<br>deleteUser<br>afterreturningnull<br>afterafter</p><hr><p>before<br>updateUser<br>throwable / by zero<br>afterafter</p><h4 id="2-5-5-3、其他"><a href="#2-5-5-3、其他" class="headerlink" title="2.5.5.3、其他"></a>2.5.5.3、其他</h4><p>异常通知，只有在产生异常后才会执行，此时，返回通知会被异常挡住不在执行。<br>返回通知可以拿到目标类方法拿到的返回值，如果没有返回值，就是 null。<br>环绕通知会使得最终通知提前。类似于，return 执行前，finally 块中的会先执行。</p><h3 id="2-5-6、补充"><a href="#2-5-6、补充" class="headerlink" title="2.5.6、补充"></a>2.5.6、补充</h3><p>全自动方式也可将切面做成类似于半自动的形式。<br>切面类：<br>前置通知：</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyAspectBefore</span> <span class="keyword">implements</span> <span class="title">MethodBeforeAdvice</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">before</span><span class="params">(Method arg0, Object[] arg1, Object arg2)</span> <span class="keyword">throws</span> Throwable </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;before&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>环绕通知：</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyAspectMethod</span> <span class="keyword">implements</span> <span class="title">MethodInterceptor</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">invoke</span><span class="params">(MethodInvocation arg0)</span> <span class="keyword">throws</span> Throwable </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;环绕前&quot;</span>);</span><br><span class="line">        Object obj = arg0.proceed();</span><br><span class="line">        System.out.println(<span class="string">&quot;环绕后&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> obj;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>配置方式：</p><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">aop:config</span> <span class="attr">proxy-target-class</span>=<span class="string">&quot;true&quot;</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 切入点 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">aop:pointcut</span> <span class="attr">expression</span>=<span class="string">&quot;execution(* aop2.UserService.*(..))&quot;</span> <span class="attr">id</span>=<span class="string">&quot;myPointCut&quot;</span> /&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 特殊的切面,只有一个环绕通知 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">aop:advisor</span> <span class="attr">advice-ref</span>=<span class="string">&quot;myAspectMethod&quot;</span> <span class="attr">pointcut-ref</span>=<span class="string">&quot;myPointCut&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">aop:advisor</span> <span class="attr">advice-ref</span>=<span class="string">&quot;myAspectBefore&quot;</span> <span class="attr">pointcut-ref</span>=<span class="string">&quot;myPointCut&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">aop:config</span>&gt;</span></span><br></pre></td></tr></table></figure></div><footer class="post-footer"><div class="post-copyright"><ul><li class="post-copyright-author"><strong>本文作者： </strong>九分石人</li><li class="post-copyright-link"><strong>本文链接：</strong> <a href="https://linqiankun.github.io/hexoblog/yuque/weidai/Spring%20AOP%E5%85%A5%E9%97%A8/" title="Spring AOP入门">https://linqiankun.github.io/hexoblog/yuque/weidai/Spring AOP入门/</a></li><li class="post-copyright-license"><strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/zh-CN" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！</li></ul></div><div class="followme"><span>欢迎关注我的其它发布渠道</span><div class="social-list"><div class="social-item"><a target="_blank" class="social-link" href="https://github.com/linqiankun"><span class="icon"><i class="fab fa-github"></i> </span><span class="label">GitHub</span></a></div><div class="social-item"><a target="_blank" class="social-link" href="https://gitee.com/linqiankun"><span class="icon"><i class="fab fa-git"></i> </span><span class="label">Gitee</span></a></div><div class="social-item"><a target="_blank" class="social-link" href="/hexoblog/images/thefive.jpg"><span class="icon"><i class="fab fa-weixin"></i> </span><span class="label">WeChat</span></a></div><div class="social-item"><a target="_blank" class="social-link" href="/hexoblog/atom.xml"><span class="icon"><i class="fa fa-rss"></i> </span><span class="label">RSS</span></a></div></div></div><div class="post-nav"><div class="post-nav-item"><a href="/hexoblog/yuque/weidai/%E9%9B%86%E5%90%88%E6%95%B4%E7%90%86%E5%8F%8A%E5%9F%BA%E7%A1%80%E6%93%8D%E4%BD%9C/" rel="prev" title="集合整理及基础操作"><i class="fa fa-chevron-left"></i> 集合整理及基础操作</a></div><div class="post-nav-item"><a href="/hexoblog/md/%E5%88%86%E5%B8%83%E5%BC%8F/%E4%B8%80%E8%87%B4%E6%80%A7hash/" rel="next" title="一致性hash">一致性hash <i class="fa fa-chevron-right"></i></a></div></div></footer></article></div><script>window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }</script></div></main><footer class="footer"><div class="footer-inner"><div class="copyright">&copy; Tue Mar 02 2021 08:00:00 GMT+0800 (中国标准时间) – <span itemprop="copyrightYear">2025</span> <span class="with-love"><i class="fa fa-heart"></i> </span><span class="author" itemprop="copyrightHolder">九分石人</span></div><div class="wordcount"><span class="post-meta-item"><span class="post-meta-item-icon"><i class="fa fa-chart-line"></i> </span><span title="站点总字数">682k</span> </span><span class="post-meta-item"><span class="post-meta-item-icon"><i class="fa fa-coffee"></i> </span><span title="站点阅读时长">10:20</span></span></div><div class="busuanzi-count"><span class="post-meta-item" id="busuanzi_container_site_uv" style="display:none"><span class="post-meta-item-icon"><i class="fa fa-user"></i> </span><span class="site-uv" title="总访客量"><span id="busuanzi_value_site_uv"></span> </span></span><span class="post-meta-item" id="busuanzi_container_site_pv" style="display:none"><span class="post-meta-item-icon"><i class="fa fa-eye"></i> </span><span class="site-pv" title="总访问量"><span id="busuanzi_value_site_pv"></span></span></span></div></div></footer><script size="300" alpha="0.6" zindex="-1" src="https://unpkg.com/ribbon.js@1.0.2/dist/ribbon.min.js"></script><script src="https://unpkg.com/animejs@3.2.1/lib/anime.min.js"></script><script src="https://unpkg.com/jquery@3.6.0/dist/jquery.min.js"></script><script src="https://unpkg.com/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js"></script><script src="https://unpkg.com/pangu@4.0.7/dist/browser/pangu.min.js"></script><script src="/hexoblog/js/utils.js"></script><script src="/hexoblog/js/motion.js"></script><script src="/hexoblog/js/next-boot.js"></script><script src="/hexoblog/js/bookmark.js"></script><script src="/hexoblog/js/local-search.js"></script><script>var mermaidElements = document.querySelectorAll('.mermaid');
if (mermaidElements.length) {
  NexT.utils.getScript('https://unpkg.com/mermaid@8.9.2/dist/mermaid.min.js', () => {
    mermaidElements.forEach(element => {
      const newElement = document.createElement('div');
      newElement.innerHTML = element.innerHTML;
      newElement.className = element.className;
      element.parentNode.replaceChild(newElement, element);
    });

    mermaid.init({
      theme    : 'default',
      logLevel : 3,
      flowchart: { curve     : 'linear' },
      gantt    : { axisFormat: '%m/%d/%Y' },
      sequence : { actorMargin: 50 }
    }, '.mermaid');
  }, window.mermaid);
}</script><script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><script src="https://unpkg.com/darkmode-js@1.5.7/lib/darkmode-js.min.js"></script><script>var options = {
  bottom: '64px',
  right: 'unset',
  left: '32px',
  time: '0.5s',
  mixColor: 'transparent',
  backgroundColor: 'transparent',
  buttonColorDark: '#100f2c',
  buttonColorLight: '#fff',
  saveInCookies: true,
  label: '🌓',
  autoMatchOsTheme: true
}
const darkmode = new Darkmode(options);
window.darkmode = darkmode;
darkmode.showWidget();</script></body></html>